package pub.util;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletWebRequest;

import pub.model.PageBean;



public class ResponseUtil {
	public static void write(Object  Msg){
		HttpServletResponse response = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();
		response.setContentType("text/html;charset=utf-8");
		try {
			PrintWriter out=response.getWriter();
			System.out.println(Msg);
			out.println(Msg.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void resultMsg(String key,String message){
		JSONObject resultMsg=new JSONObject();
		resultMsg.put(key, message);
		ResponseUtil.write(resultMsg);
	}
	public static void resultMsg(Map<String, String> map){
		JSONObject resultMsg=JSONObject.fromObject(map);
		ResponseUtil.write(resultMsg);
	}
	public static void resultForm(Object object){
		JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonDateValueProcessor());
		JSONObject form=JSONObject.fromObject(object,jsonConfig);
		ResponseUtil.write(form);
	}
	public static <T> JSONObject writePage(PageBean<T> page){
		JSONObject resultPage=new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Timestamp.class , new JsonDateValueProcessor());
		//JSONArray data= JSONArray.fromObject(page.getResult(),jsonConfig);
		resultPage.put("iTotalRecords", page.getTotal());
		resultPage.put("iTotalDisplayRecords", page.getTotal());
		resultPage.put("aaData", page.getResult());
		System.out.println(resultPage);
		return resultPage;
	}
	public static <T> String writeGrid(List<T> list){
		JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());
		JSONArray rows= JSONArray.fromObject(list,jsonConfig);
		System.out.println(rows.toString());
		return rows.toString();
		//ResponseUtil.write(resultGrid);
	}
	public static <T> void writeJSON(List<T> list){
		JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());
		JSONArray resulJSON=JSONArray.fromObject(list,jsonConfig);
		ResponseUtil.write(resulJSON);
	}
	public static <T> void writeJSON(Map map){
		JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());
		JSONArray resulJSON=JSONArray.fromObject(map,jsonConfig);
		ResponseUtil.write(resulJSON);
	}
}
